package com.zc.blog.controller.global;

import com.zc.blog.model.*;
import com.zc.blog.service.IArticleService;
import com.zc.blog.service.ICategoryService;
import com.zc.blog.service.ITagService;
import com.zc.blog.service.IUserService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.List;

/**
 * 全局参数设置（优先于controller）
 */
@ControllerAdvice
public class GlobalControllerAdvice {

    @Autowired
    private IUserService userService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private ITagService tagService;

    @Autowired
    private IArticleService articleService;

    @ModelAttribute
    public void addAttribute(Model model, HttpServletRequest request, HttpSession session) {
        User user = userService.findById(2); //获取用户
        List<Category> categoryShowList = categoryService.findShow();            //获取显示的分类列表
        List<Tag> tagShowList = tagService.findShow();                           //获取现实的标签列表
        List<Article> topHotArticleList = articleService.findHotArticles();          //获取热门文章
        Visitor getVisitor = (Visitor) session.getAttribute("getVisitor");
        model.addAttribute("getVisitor", getVisitor);
        model.addAttribute("user", user);                           //用户
        model.addAttribute("categoryShowList", categoryShowList);   //分类显示列表
        model.addAttribute("tagShowList", tagShowList);             //标签显示列表
        model.addAttribute("requestURI", request.getRequestURI());  //传递当前url
        model.addAttribute("topHotArticleList", topHotArticleList);         //热门文章
    }
}
